/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package argtrust.xml;
import java.io.*;

//import edu.cuny.argtrust.argsys.Argument;
//import edu.cuny.argtrust.argsys.Argumentation;
//import edu.cuny.argtrust.logic.InferenceRule;
//import edu.cuny.argtrust.logic.ReasoningLink;
//import edu.cuny.argtrust.logic.Sentence;
//import edu.cuny.argtrust.logic.Term;
import org.apache.xerces.parsers.DOMParser;
import java.util.HashMap;
import org.w3c.dom.*;

/**
 *
 * @deprecated 
 * @author Yuqing Tang <ytang@cs.gc.cuny.edu>
 */
public class ArgTrustXMLReader {

    ///A map from id to the entity.
    protected HashMap mEntityTable;

    public void load(String file_name) {
        try {
            DOMParser p = new DOMParser();
            p.parse(file_name);
            Document doc = p.getDocument();
//            Node n = doc.getFirstChild();
            Node n = doc.getDocumentElement().getFirstChild();
            while (n != null) {
                process(n);
                //Get next sibling node
                n = n.getNextSibling();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void process(Node node) {
        { //Default: recursively processing the nodes
            print(node);
            Node n = node.getFirstChild();
            while (n != null) {
                process(n);
                //Get next sibling node
                n = n.getNextSibling();
            }
        }
    }

    public void print(Node node) {

        System.out.println(node.getNodeName()
                + ": " + this.getAttribute(node, "id")
                //                + "= " + node.getNodeValue()
                + "= " + this.getNodeTextValue(node));
    }

    //------------Helper functions ----------------
    public String getAttribute(Node node, String name) {
        NamedNodeMap attributes = (NamedNodeMap) node.getAttributes();
        if (attributes == null) {
            return null;
        }
        Node attribute = attributes.getNamedItem(name);
        if (attribute == null) {
            return null;
        }
        return attribute.getNodeValue();

    }

    public String getNodeTextValue(Node node) {
        NodeList nlList = node.getChildNodes();
        Node nValue = (Node) nlList.item(0);
        if (nValue == null) {
            return null;
        }
        return nValue.getNodeValue();
    }

    public String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }
}
